<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
      @click="onPost"
      :disabled="!message.length"
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/apis/comment'
import { showFailToast, showLoadingToast, showSuccessToast } from 'vant'
export default {
    props: {
        target: {
            type: [Number, String, Object],
            required: true
        }
    },
    inject: {
      articleId: {
        type: [String, Object],
        default: null
      }
    },
    data () {
        return {
        message: ''
        }
    },
    methods: {
        async onPost() {
            showLoadingToast('发布中')
            try {
                const {data} = await addComment({
                    target:this.target,//文章id 或者 评论id
                    content: this.message,
                    art_id: this.articleId ? this.articleId.toString() : null
                })
                console.log(data);
                this.message = ''
                this.$emit('post-success',data.data)
                showSuccessToast('发布成功')
            } catch (error) {
                showFailToast('发布失败',error)
            }
        }
    }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>